package edu.kit.csl.pisa.training.eva2;

/*
This file is part of the PISA Alignment Tool.

Copyright (C) 2013
Karlsruhe Institute of Technology
Cognitive Systems Lab (CSL)
Felix Stahlberg

PISA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PISA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PISA. If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.Serializable;

import edu.kit.csl.pisa.datatypes.SentenceAlignment;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.problems.InterfaceOptimizationProblem;

/**
 * This individual uses genotypes in form of {@link SentenceAlignment}
 * instances.
 */
public class IndividualSentenceAlignment extends AbstractEAIndividual implements
		Serializable, InterfaceDataTypeSentenceAlignment {

	private static final long serialVersionUID = -3401454471110948812L;
	
	private SentenceAlignment align;

	/**
	 * Use this constructor to initialize the genotype directly.
	 * 
	 * @param align the genotype
	 */
	public IndividualSentenceAlignment(SentenceAlignment align) {
		this.align = align;
	}
	
	public IndividualSentenceAlignment() {
		this.align = null;
	}
	
    public IndividualSentenceAlignment
    			(IndividualSentenceAlignment individual) {
    	if (null == individual.align) {
    		this.align = null;
    	} else {
    		this.align = (SentenceAlignment) individual.align.clone();
    	}
        // Cloning the members of AbstractEAIndividual
        this.m_Age = individual.m_Age;
        this.m_CrossoverOperator = individual.m_CrossoverOperator;
        this.m_CrossoverProbability = individual.m_CrossoverProbability;
        this.m_MutationOperator = individual.m_MutationOperator;
        this.m_MutationProbability = individual.m_MutationProbability;
        this.m_SelectionProbability = 
        		individual.m_SelectionProbability.clone();
        this.m_Fitness = new double[]{individual.m_Fitness[0]};
        cloneAEAObjects((AbstractEAIndividual) individual);
    }
	
	/**
	 * Should perform a standard mutation on the individual. Since in our case,
	 * the mutation operators are always predefined, this method should never
	 * be called.
	 * 
	 * @throws UnsupportedOperationException unconditionally
	 */
	@Override
	public void defaultMutate() {
		throw new UnsupportedOperationException();
	}

	/**
	 * Should initialize the individual randomly. Since in our case, the
	 * initial population is always known, this method should never be called.
	 * 
	 * @throws UnsupportedOperationException unconditionally
	 */
	@Override
	public void defaultInit(InterfaceOptimizationProblem prob) {
		;//throw new UnsupportedOperationException();
	}

	/**
	 * Set {@link SentenceAlignment} phenotype.
	 * 
	 * @param align new phenotype
	 */
	@Override
	public void setPhenotype(SentenceAlignment align) {
		this.align = align;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see #setPhenotype(SentenceAlignment)
	 */
	@Override
	public void setGenotype(SentenceAlignment align) {
		this.align = align;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SentenceAlignment getSentenceAlignment() {
		return align;
	}

	/**
	 * Clones the individual. The genotype is cloned as well, so the clone will
	 * be completely independent from its parent.
	 */
	@Override
	public Object clone() {
		IndividualSentenceAlignment indy = new IndividualSentenceAlignment(this);
		if (align != null) {
			indy.setGenotype((SentenceAlignment) align.clone());
		}
		return indy;
	}

	/**
	 * Returns true if the given individual has the same genotype.
	 * 
	 * @param individual the individual to compare
	 * @returns true if the individual has the same genotype, false otherwise
	 * @see edu.kit.csl.pisa.datatypes.SentenceAlignment#equals(Object)
	 */
	@Override
	public boolean equalGenotypes(AbstractEAIndividual individual) {
		if (individual instanceof IndividualSentenceAlignment) {
			return ((IndividualSentenceAlignment) individual)
					.getSentenceAlignment().equals(align);
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initByValue(Object obj, InterfaceOptimizationProblem opt) {
		align = (SentenceAlignment) obj;
	}

	/**
	 * This methods returns a string description for this individual.
	 * 
	 * @see edu.kit.csl.pisa.datatypes.SentenceAlignment#toString()
	 */
	@Override
	public String getStringRepresentation() {
		return toString();
	}
	
	

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "SenAlignmentIndividual\n" + (align == null ? "null"
				: align.toString());
	}
}
